package interfaces.hud.end;

import java.util.ArrayList;
import java.util.List;

import interfaces.GUISource;
import interfaces.language.LanguageButton;
import interfaces.superWidgets.InterfaceLabel;
import interfaces.superWidgets.InterfaceWindow;
import interfaces.superWidgets.tabs.HorizontalTabContent;
import logic.common.game.Game;
import logic.common.team.Team;

import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.layout.StaticLayout;
import org.fenggui.util.Color;

import fileHandling.language.Lang;
import fileHandling.language.LanguageLoader;
import fileHandling.language.interfaces.EndWindowText;
import gameStates.gui.GUIState;

public class EndWindow extends InterfaceWindow {
	
	private List<InterfaceWindow> tabWindows;
	private ScoreBoardWindow showPlayersWindow;
	private PlayerStatsWindow playerStatsWindow;
	
	private Team winnerTeam;
	private InterfaceLabel countDownLabel;
	private GUIState guiState;
	private HorizontalTabContent tabContent;
	private InterfaceWindow currentWindow;
	private Game game;
	
	private int windowWidth, windowHeight, windowX, windowY;
	
	public EndWindow(Game game, Team winnerTeam, GUIState guiState) {
		super(false, "");
		this.guiState = guiState;
		this.winnerTeam = winnerTeam;
		this.game = game;
		
		int border = guiState.getHeight() / ScoreBoardWindow.BORDER_FACTOR;
		int width = guiState.getWidth() - (2 * border);
		int height = border * 2;
		int x = border;
		int y = guiState.getHeight() - border - height;
		setSize(width, height);
		setXY(x, y);
		
		tabContent = new HorizontalTabContent(content.getWidth(), content.getHeight() / 2);
		content.setLayoutManager(new StaticLayout());
		content.addWidget(tabContent);
		
		tabWindows = new ArrayList<InterfaceWindow>();
		
		windowWidth = width;
		windowHeight = y - (2 * border);
		windowX = border;
		windowY = border;
		
		showPlayersWindow = new ScoreBoardWindow(game, windowWidth, windowHeight, windowX, windowY, guiState);
		addTabWindow(showPlayersWindow, EndWindowText.Scoreboard);
		
		setCurrentWindow(showPlayersWindow);
		
		playerStatsWindow = new PlayerStatsWindow(game, windowWidth, windowHeight, windowX, windowY);
		addTabWindow(playerStatsWindow, EndWindowText.Player_Stats);
		
		makeLabels(content.getHeight() / 2);
		
		layout();
	}
	
	private void addTabWindow(final InterfaceWindow window, Lang text) {
		tabWindows.add(window);
		
		LanguageButton button = new LanguageButton(text, GUISource.bigFont);
		button.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				setCurrentWindow(window);
			}
		});
		tabContent.addTabButton(button);
	}
	
	private void setCurrentWindow(InterfaceWindow window) {
		if(currentWindow == window) return;
		if(currentWindow != null) guiState.removeWidget(currentWindow);
		
		currentWindow = window;
		guiState.addWidget(currentWindow);
	}
	
	private void makeLabels(int height) {
		int y = height;
		int border = content.getWidth() / 20;
		
		String teamLabel = " ";
		if(winnerTeam != null) 
			teamLabel = LanguageLoader.get(EndWindowText.The) + " " + winnerTeam.getFraction() + "s ";
		
		String winnerString = teamLabel + LanguageLoader.get(EndWindowText.won_this_game);
		InterfaceLabel winnerLabel = new InterfaceLabel(winnerString, GUISource.bigFont);
		GUISource.setTextColor(winnerLabel, Color.BLUE);
		winnerLabel.setSize(content.getWidth() / 3, height);
		winnerLabel.setXY(border, y);
		content.addWidget(winnerLabel);
		
		String cdString = LanguageLoader.get(EndWindowText.Next_map_in) + game.getCoundDown();
		countDownLabel = new InterfaceLabel(cdString, GUISource.bigFont);
		GUISource.setTextColor(countDownLabel, Color.BLACK);
		countDownLabel.setSize(content.getWidth() / 6, height);
		countDownLabel.setXY(content.getWidth() - countDownLabel.getWidth() - border, y);
		content.addWidget(countDownLabel);
	}
	
	public void updateCountDown(int seconds) {
		countDownLabel.setText(LanguageLoader.get(EndWindowText.Next_map_in) + " " + seconds);
	}
}